package com.cooper.service.item;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.constants.AppConstants;
import com.cooper.dao.item.ItemInfoDAO;
import com.cooper.entity.ItemInfo;
import com.cooper.pagination.PageInfo;
import com.cooper.resource.IResourceManager;
import com.cooper.vo.CompositeDesignerItemInfoVo;
import com.cooper.vo.DisplayItemInfoVo;
import com.cooper.vo.ItemInfoVo;
import com.cooper.vo.ItemSearchVo;

@Service
public class ItemInfoService {
	
	@Autowired
	private IResourceManager resourceManager;
	
	@Autowired
	private ItemInfoDAO itemInfoDao;
	
	public long saveEntity(ItemInfo itemInfo) throws Exception{
		return this.itemInfoDao.saveEntity(itemInfo);
	}

	public boolean updateEntity(ItemInfo itemInfo) throws Exception{
		return this.itemInfoDao.updateEntity(itemInfo);
	}
	
	public boolean deleteEntity(ItemInfo itemInfo) throws Exception{
		return this.itemInfoDao.deleteEntity(itemInfo);
	}
	
	public ItemInfo loadById(long itemId) throws Exception{
		return this.itemInfoDao.loadById(itemId);
	}

	public int queryItemInfoCount(Long userId, ItemSearchVo searchVo) throws Exception{
		return this.itemInfoDao.queryItemInfoCount(userId, searchVo);
	}
	
	public List<ItemInfoVo> queryItemInfoList(Long userId, ItemSearchVo searchVo, PageInfo pageInfo) throws Exception{
		List<ItemInfoVo> itemList = this.itemInfoDao.queryItemInfoList(userId, searchVo, pageInfo);
		if(CollectionUtils.isNotEmpty(itemList)){
			for(int index=0; index < itemList.size(); index++){
				ItemInfoVo itemVo = itemList.get(index);
				Object soldObj = this.resourceManager.getResItem(AppConstants.ITEM_SOLD_NUM, String.valueOf(itemVo.getId()));
				if(null == soldObj){
					itemVo.setSold(0);
				} else {
					itemVo.setSold(Integer.parseInt(String.valueOf(soldObj)));
				}
				itemList.set(index, itemVo);
			}
		}
		return itemList;
	}
	
	public List<ItemInfoVo> getRelatedItemList(Long userId, Long itemId) throws Exception{
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageSize(4);
		return this.itemInfoDao.getRelatedItemList(userId, itemId, pageInfo);
	}
	
	public List<DisplayItemInfoVo> queryTopSoldOutItems(PageInfo pageInfo) throws Exception{
		return this.itemInfoDao.queryTopSoldOutItems(pageInfo);
	}
	
	
	public List<ItemInfoVo> querySelectTopSoldOutItems(PageInfo pageInfo) throws Exception{
		return this.itemInfoDao.querySelectTopSoldOutItems(pageInfo);
	}
	
	public int queryTopSoldOutItemsCount() throws Exception{
		return this.itemInfoDao.queryTopSoldOutItemsCount();
	}
	
	public List<CompositeDesignerItemInfoVo> findCompositeDesignerItemInfosByItemIds(List<String> itemIds) throws Exception{
		return this.itemInfoDao.findCompositeDesignerItemInfosByItemIds(itemIds);
	}
	
	public CompositeDesignerItemInfoVo findCompositeDesignerItemInfosByItemIds(String itemId) throws Exception{
		List<String> items = new ArrayList<String>();
		items.add(itemId);
		List<CompositeDesignerItemInfoVo> ret = this.itemInfoDao.findCompositeDesignerItemInfosByItemIds(items);
		if(ret!=null){
			return ret.get(0);
		}
		return null;
	}
	
	public List<ItemInfoVo> querySomethingLike(long cid, long sellerId, PageInfo pageInfo) throws Exception{
		return this.itemInfoDao.querySomethingLike(cid, sellerId, pageInfo);
	}
}
